﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.BLL.InfoConfig;

namespace MINERP.InfoConfig
{
    public partial class FrmComplexSet : Form
    {
        public FrmComplexSet()
        {
            InitializeComponent();
        }
        ComplexProductBll _bll = new ComplexProductBll();
        private static FrmComplexSet instance = null;

        public static FrmComplexSet Instance
        {
            get
            {
                if (instance == null)
                    instance = new FrmComplexSet();
                return instance;
            }

        }

        private void FrmComplexSet_Load(object sender, EventArgs e)
        {
            EventMonitor.MonitoringSelectChange += SubProductSelectChange;
            EventMonitor.MonitoringCancelChange += SubProductCancelChange;
           RushMain();
           buttonModefy.Enabled = true;
           buttonSave.Enabled = false;
           buttonCancel.Enabled = true;
           AllSunProduct = _bll.GetAllSubProduct();
        }
        void SubProductSelectChange(object sender, MonitorEventArgs e)
        {
            SubProductInfo spi = (SubProductInfo)e.args;
            ComplexProductInfo com = currComplexProductInfo;
            if (com == null)
            {
                MessageBox.Show("请重新选择要修改的组合产品");
                return;
            }
            com.SubProducts.Add(spi);

        }
        void SubProductCancelChange(object sender, MonitorEventArgs e)
        {
            SubProductInfo spi = (SubProductInfo)e.args;
            ComplexProductInfo com = currComplexProductInfo;
            if (com == null)
            {
                MessageBox.Show("请重新选择要修改的组合产品");
            }
            com.SubProducts.Remove(spi);

        }
        void RushMain()
        {
            gridControlMainProduct.DataSource = _bll.GetAllComplexProduct();
            gridControlMainProduct.RefreshDataSource();

            ComplexProductInfo com = (ComplexProductInfo)gridViewMainProduct.GetFocusedRow();
            gridControlSubProduct.DataSource = com.SubProducts;
            gridControlSubProduct.RefreshDataSource();
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            gridColumnSubSelect.OptionsColumn.AllowEdit = false;
            gridColumnSubNum.OptionsColumn.AllowEdit = false;
            buttonModefy.Enabled = true;
            buttonSave.Enabled = false;
            List<ComplexProductInfo> list = (List<ComplexProductInfo>)gridControlMainProduct.DataSource;
            _bll.UpdateProduct(list.Where(p => p.IsEdit).ToList());
            isModify = false;
            RushMain();
        }
        bool isModify = false;
        List<SubProductInfo> AllSunProduct;
        private void buttonModefy_Click(object sender, EventArgs e)
        {
            isModify = true;
            gridColumnSubSelect.OptionsColumn.AllowEdit = true;
            gridColumnSubNum.OptionsColumn.AllowEdit = true;
           
            buttonModefy.Enabled = false;
            buttonSave.Enabled = true;

        }
        ComplexProductInfo currComplexProductInfo=null;
        private void gridViewMainProduct_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            if (e.RowHandle >= 0)
            {              
                ComplexProductInfo cpi = (ComplexProductInfo)gridViewMainProduct.GetRow(e.RowHandle);
                currComplexProductInfo = cpi;
                if (isModify)
                {
                    //AllSunProduct.ForEach(f => f.isOld = false);
                    //AllSunProduct.ForEach(f => f.isSelect = false);
                    //AllSunProduct.ForEach(f => f.isAdd = false);
                    //AllSunProduct.ForEach(f => f.isEdit = false);
                    List<SubProductInfo> list = new List<SubProductInfo>();
                    gridControlSubProduct.DataSource = list;
                    gridControlSubProduct.RefreshDataSource();
                    foreach (SubProductInfo aspi in AllSunProduct)
                    {
                        SubProductInfo contain = cpi.SubProducts.FirstOrDefault(p => p.ContainProductid == aspi.ProductId);
                        if (contain != null)
                        {
                            list.Add(contain);
                        }
                        else
                        {
                            list.Add(aspi);
                        }
                        
                    }

                    gridControlSubProduct.DataSource = list;
                    gridControlSubProduct.RefreshDataSource();
                }
                else
                {
                    gridControlSubProduct.DataSource = cpi.SubProducts;
                    gridControlSubProduct.RefreshDataSource();
                }
            }
        }

        private void FrmComplexSet_FormClosing(object sender, FormClosingEventArgs e)
        {
            instance = null;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            buttonModefy.Enabled = true;
            buttonSave.Enabled = false;
            isModify = false;
        }

        private void gridViewSubProduct_CellValueChanging(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if (e.RowHandle >= 0 && e.Column.Name == "gridColumnSubSelect")
            {
                SubProductInfo spi = (SubProductInfo)gridViewSubProduct.GetRow(e.RowHandle);
                spi.isSelect = (bool)e.Value;
            }

        }

        private void gridViewSubProduct_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            
        }

    }
}
